﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace LeafHttp.TestWebapi.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class TestController : ControllerBase
    {
        private readonly ILogger<TestController> _logger;

        private static string _name = "未命名";

        public TestController(ILogger<TestController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public ActionResult GetName()
        {
            return Content(_name);
        }

        [HttpPost]
        public void UpdateName([FromForm] string name)
        {
            _name = name;
        }

        [HttpGet]
        public ActionResult DownloadFile()
        {
            var fileBytes = System.IO.File.ReadAllBytes("download.jpg");
            return File(fileBytes, "image/jpeg");
        }

        [HttpPost]
        public void UploadFile(IFormFile file)
        {
            var savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.FileName);
            using (FileStream fs = new FileStream(savePath, FileMode.Create))
            {
                file.CopyTo(fs);
            }
        }
    }
}
